package adminServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swim2.sessionbeans.*;

public class ReadAbRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public ReadAbRequestServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		try{
			Context ctx = getInitialContext();			
			AdminSystemRemote adminSystemRemote = (AdminSystemRemote) ctx.lookup("adminJNDI");
			String adminId = request.getParameter("adminId");
			String adminMail = request.getParameter("adminMail");
			String adminPassword = request.getParameter("adminPassword");
			String requestName = request.getParameter("abReqName");
			
			ArrayList<String> abRequest = adminSystemRemote.getAbRequestData(requestName);
			adminSystemRemote.setViewedRequest(requestName);
			
			request.setAttribute("reqId", abRequest.get(0));
			request.setAttribute("reqName", abRequest.get(1));
			request.setAttribute("reqDescr", abRequest.get(2));
			request.setAttribute("reqUserName", abRequest.get(3));
			request.setAttribute("reqUserSurname", abRequest.get(4));
			request.setAttribute("adminId", adminId);
			request.setAttribute("adminMail", adminMail);
			request.setAttribute("adminPassword", adminPassword);
			RequestDispatcher dispatcher =
			request.getRequestDispatcher("/AdminJsp/ReadAbRequest.jsp");
			dispatcher.forward(request, response);	
		}
		catch (Exception e) {
			e.printStackTrace();
			out.print("Eccezione: errore nel caricamento della pagina!");
		}
	}

	static public Context getInitialContext() throws Exception{
		Hashtable<String,String> env = new Hashtable<String,String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		env.put(Context.PROVIDER_URL, "localhost:1099");
		return new InitialContext(env);
	}
}

